package qrcode

import (
	"encoding/base64"
	"github.com/gin-gonic/gin"
	"github.com/skip2/go-qrcode"
	"net/http"
)

type params struct {
	URL string `form:"url" binding:"required"`
}

func Gen(c *gin.Context) {
	var params params
	if err := c.ShouldBind(&params); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 1,
			"msg":  err.Error(),
		})
		return
	}

	//生成二维码
	var png []byte
	var err error
	if png, err = qrcode.Encode(params.URL, qrcode.Medium, 256); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 1,
			"msg":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"data": "data:image/png;base64," + base64.StdEncoding.EncodeToString(png),
		"msg":  "success",
	})
}
